473,465 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to dynamically change master page?

ad
Hi,
How can I dynamically change the MasterPager of a web page?
Jun 17 '06 #1
9 16257
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:O9****************@TK2MSFTNGP02.phx.gbl...
How can I dynamically change the MasterPager of a web page?


private void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MyNewPasterPage.master" // amend as
appropriate
}
Jun 17 '06 #2
The MasterPage does not support the Page_PreInit so it has to be used in
each content page or a base class must be used which all content pages
inherit from.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> wrote in message
news:OG**************@TK2MSFTNGP04.phx.gbl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:O9****************@TK2MSFTNGP02.phx.gbl...
How can I dynamically change the MasterPager of a web page?


private void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MyNewPasterPage.master" // amend as
appropriate
}

Jun 18 '06 #3
"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:O4**************@TK2MSFTNGP03.phx.gbl...
The MasterPage does not support the Page_PreInit
I didn't say it did...
so it has to be used in each content page or a base class must be used
which all content pages inherit from.


Correct.
Jun 18 '06 #4
ad
Thanks,
But if I wish when an user press a button on web page, it will do the change
of mastPage.
How can I do that?
"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> ¼¶¼g©ó¶l¥ó·s»D:OG**************@TK2MSFTNGP04.phx.g bl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:O9****************@TK2MSFTNGP02.phx.gbl...
How can I dynamically change the MasterPager of a web page?


private void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MyNewPasterPage.master" // amend as
appropriate
}

Jun 19 '06 #5
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:Oq**************@TK2MSFTNGP05.phx.gbl...
Thanks,
But if I wish when an user press a button on web page, it will do the
change of mastPage.
How can I do that?


Do you want the selected MasterPage to be for the whole site (for the
current user), or just for the current page?
Jun 19 '06 #6
ad
For just one page, and change it for a while.
"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> ¼¶¼g©ó¶l¥ó·s»D:%2****************@TK2MSFTNGP05.phx .gbl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:Oq**************@TK2MSFTNGP05.phx.gbl...
Thanks,
But if I wish when an user press a button on web page, it will do the
change of mastPage.
How can I do that?


Do you want the selected MasterPage to be for the whole site (for the
current user), or just for the current page?

Jun 19 '06 #7
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e6**************@TK2MSFTNGP05.phx.gbl...
For just one page, and change it for a while.


OK, then.

The *only* place that a content page can set its MasterPage is in its
Page_PreInit method, so you're going to have to do the following:

For each content page for which you want to enable dynamic MasterPages,
you'll need to set up a Session variable, which you'll need to initialise on
the Session_Start method in global.asax.

1) E.g. let's say you have a page called MyPage.aspx - in Session_Start,
you'll need to do the following:

Session["MyPage_Master"] = "~/master/Master1.master";

2) In the Page_PreInit of MyPage.aspx, you'll need to do the following:

private void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = Session["MyPage_Master"].ToString();
}

3) To change the MasterPage, you'll simply need to create an <asp:Button> or
any other webcontrol which can cause a postback, set the
Session["MyPage_Master"] session variable to the new MasterPage, then
Response.Redirect the page back to itself.

Thereafter, whenever MyPage.aspx is called, it will set its MasterPage to
whatever value is in the Session["MyPage_Master"] session variable.

Sounds a bit convoluted, but it's fairly straightforward once you get your
head round it... :-) Good luck!
Jun 19 '06 #8
ad
Thanks,
Your answer is great, but I have a question about your answer.
Why we need to Response.Redirect the page back to itself after change the
Session variable of MasgerPage.
I have test if it didn't Response.Redirect the page back to itself, just
let the page postback, the MasterPage will not change at the fist postback,
I must postbak twice to make the change.

Why?


"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> ¼¶¼g©ó¶l¥ó·s»D:uT****************@TK2MSFTNGP04.phx .gbl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e6**************@TK2MSFTNGP05.phx.gbl...
For just one page, and change it for a while.


OK, then.

The *only* place that a content page can set its MasterPage is in its
Page_PreInit method, so you're going to have to do the following:

For each content page for which you want to enable dynamic MasterPages,
you'll need to set up a Session variable, which you'll need to initialise
on the Session_Start method in global.asax.

1) E.g. let's say you have a page called MyPage.aspx - in Session_Start,
you'll need to do the following:

Session["MyPage_Master"] = "~/master/Master1.master";

2) In the Page_PreInit of MyPage.aspx, you'll need to do the following:

private void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = Session["MyPage_Master"].ToString();
}

3) To change the MasterPage, you'll simply need to create an <asp:Button>
or any other webcontrol which can cause a postback, set the
Session["MyPage_Master"] session variable to the new MasterPage, then
Response.Redirect the page back to itself.

Thereafter, whenever MyPage.aspx is called, it will set its MasterPage to
whatever value is in the Session["MyPage_Master"] session variable.

Sounds a bit convoluted, but it's fairly straightforward once you get your
head round it... :-) Good luck!

Jun 20 '06 #9
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:uh**************@TK2MSFTNGP05.phx.gbl...
Your answer is great, but I have a question about your answer.
Why we need to Response.Redirect the page back to itself after change the
Session variable of MasgerPage.
I have test if it didn't Response.Redirect the page back to itself, just
let the page postback, the MasterPage will not change at the fist
postback, I must postbak twice to make the change.

Why?


Because the MasterPage can *only* be set in a content page's Page_PreInit
method.
Jun 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob Roberts | last post by:
In a VS2005 ASP.NET project, I'm trying to find a way to change which css file is linked in based on the browser type. I'd like to use one css file for IE browsers, and a different one for all...
3
by: Ron | last post by:
Hi all, I've come across a problem when casting user controls in ASP.NET 2.0, hopefully someone can help. I have a master page with a user control on it (myMenu) that I wish to programmatically...
2
by: Mark Rae | last post by:
Hi, It's easy enough to get a content page to refer to public properties on its associated master page e.g. public partial class master_secure : System.Web.UI.MasterPage { public Label...
4
by: David Lozzi | last post by:
Howdy, I have my master page, and I would like to change the background CSS class per the content page. Only the home page has a different background style, all other pages are using the same. I...
12
by: Mark Rae | last post by:
Hi, It's easy enough for a child page to manipulate its MasterPage's header simply by modifying the MasterPage thus: <header runat="server"> .... .... </header>
23
by: LvBohemian | last post by:
I am playing around with creating some menus dynamically, and they create fine and show up ok when I want them to; but when I select a menu NavigateUrl at run time, the applicable url shows up...
1
by: Dinu | last post by:
How to Dynamically program User controls in a Master page from a content page Thanks
4
by: CJM | last post by:
I have a site that is being built using master pages, and includes a menu that I want to manipulate dynamically. Specifically the currently-selected menu option is styled differently to the...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.